﻿package com.autotag.nlp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 产生单词对应词根关系MAP的工具类
 * @author Wang Leye
 * @date 2010.12.13
 */
public class WordRootMap {
	
	/**
	 * 根据文件产生单词和词根对应的MAP
	 * @param rootMapFile 存储对应关系的文本文件
	 * @return 单词对应词根的Map对象
	 * @throws FileNotFoundException 
	 */
	public static Map<String, String> readMapFile(File rootMapFile) throws FileNotFoundException {
		Map<String, String> rootMap = new HashMap<String, String>();
		BufferedReader reader = new BufferedReader(new FileReader(rootMapFile));
		String line;
		String currentRoot = null;
		//读文件
		try {
			while ((line=reader.readLine())!=null) {
				if (isRoot(line)) {		//当前行是词根
					currentRoot = getRoot(line);
					
					if (currentRoot.indexOf("->") > 0){
						int borderPos = currentRoot.indexOf("->");
						String firstStr = new String(currentRoot.substring(0, borderPos));
						int secondBeginPos = currentRoot.indexOf("[", borderPos);
						int secondEndPos = currentRoot.indexOf("]", borderPos);
						String secondStr = new String(currentRoot.substring(borderPos + 2));
						if (secondBeginPos >= 0 && secondEndPos > secondBeginPos + 1){
							secondStr = new String(currentRoot.substring(secondBeginPos+1, secondEndPos));
						}
						currentRoot = secondStr;
						rootMap.put(firstStr, currentRoot);
						rootMap.put(secondStr, currentRoot);
					}else{
						rootMap.put(currentRoot, currentRoot);
					}
					
				}
				else {		//否则当前行是单词
					String[] words = getWords(line);
					for (String word: words) {
						if (word.indexOf("->") > 0){
							int borderPos = word.indexOf("->");
							String firstStr = new String(word.substring(0, borderPos));
							int secondBeginPos = word.indexOf("[", borderPos);
							int secondEndPos = word.indexOf("]", borderPos);
							String secondStr = new String(word.substring(borderPos + 2));
							if (secondBeginPos >= 0 && secondEndPos > secondBeginPos + 1){
								secondStr = new String(word.substring(secondBeginPos+1, secondEndPos));
							}
							rootMap.put(secondStr, currentRoot);
							rootMap.put(firstStr, currentRoot);
						}else{
							rootMap.put(word, currentRoot);
						}
					}
				}
			}
			return rootMap;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("文件读取错误");
			e.printStackTrace();
			return null;
		}
	}

	/*
	 * 目前处理的文本格式：
	 * a
	 *     an
	 * aardvark
	 *     aardvarks
	 * abandon
     *     abandoned, abandoning, abandons
	 * ......
	 */
	
	//直接取就行了
	private static String getRoot(String line) {
		return line.trim();
	}

	//按逗号空格切分
	private static String[] getWords(String line) {
		return line.trim().split(",\\s+");
	}

	//如果当前行前无空格，就是词根
	private static boolean isRoot(String line) {
		if (line.startsWith(" "))
			return false;
		else
			return true;
	}
	
	public static void main(String[] args) throws FileNotFoundException {
		Map<String, String> rootMap =
			WordRootMap.readMapFile(new File("models/2lemma.txt"));
		test("an", rootMap);
		test("abandons", rootMap);
		test("stymy", rootMap);
		test("verses", rootMap);
	}
	
	private static void test(String word, Map<String, String> rootMap) {
		System.out.println(word+"->"+rootMap.get(word));
	}
}
